package com.sms.service;


import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sms.controller.IndustrySMS;
import com.system.comm.util.DateUtils;
import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisCustomUserMapper;
import com.system.common.mapper.BisSmsRecordMapper;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisSmsRecord;
import com.system.common.pojo.BisSmsRecordExample;
import com.system.common.pojo.BisSmsRecordExample.Criteria;

@Service
public class BisSmsRecordServiceImpl implements BisSmsRecordService{
	@Autowired
	private BisSmsRecordMapper bisSmsRecordMapper;
	@Autowired
	private BisCustomUserMapper bisCustomUserMapper;
	@Override
	public List<BisSmsRecord> selectAll(Map<String,Object> map) {
		String  mobile=(String) map.get("mobile");
		String  content=(String) map.get("content");
		String  smsTypes=(String) map.get("smsType");
		String  createTime=(String) map.get("createTime");
		String  endTime=(String) map.get("endTime");
		String  sendTime=(String) map.get("sendTime");
		String  sendEndTime=(String) map.get("sendEndTime");
		String  status=(String) map.get("status");
		//初始化一个查询条件
		BisSmsRecordExample example=new BisSmsRecordExample();
		Criteria criteria=example.createCriteria();
		if (StringUtils.isNotBlank(mobile)) {
			criteria.andMobileLike("%"+mobile+"%");
		}
		if (StringUtils.isNotBlank(content)) {
			criteria.andContentLike("%"+content+"%");
		}
		if (StringUtils.isNotBlank(smsTypes)) {
			criteria.andSmsTypeEqualTo(smsTypes);
		}
		if (StringUtils.isNotBlank(status)) {
			criteria.andStatusEqualTo(status);
		}
		if (StringUtils.isNotBlank(createTime) &&StringUtils.isNotBlank(endTime)) {
			criteria.andCreateTimeBetween(DateUtils.getYmdDate(createTime),DateUtils.getYmdDate(endTime));
		}
		if (StringUtils.isNotBlank(sendTime) &&StringUtils.isNotBlank(sendEndTime)) {
			criteria.andCreateTimeBetween(DateUtils.getYmdDate(sendTime),DateUtils.getYmdDate(sendEndTime));
		}
		List<BisSmsRecord> list=bisSmsRecordMapper.selectByExample(example);
		return list;
	}

	@Override
	public void delete(String recordId) {
		String[] arr=recordId.split(",");
		for(int i=0;i<arr.length;i++){
			if(arr[i]!=null&&!arr[i].equals("")){
				bisSmsRecordMapper.deleteByPrimaryKey(String.valueOf(arr[i]));
			}
		}
	}

	@Override
	public BisSmsRecord getById(String recordId) {
		return bisSmsRecordMapper.selectByPrimaryKey(recordId);
	}

	@Override
	public void save(String address, String content) {
		boolean state=IndustrySMS.execute(address,content);
		if (state) {
		BisSmsRecord record=new BisSmsRecord(); 
		Snowflake snowflake=new Snowflake(0, 0);
		Long lid=snowflake.nextId();
		String id=lid+"";
		record.setRecordId(id);
		record.setMobile(address);
		record.setContent(content);
		record.setSmsType("2");
		record.setSendUserId(null);
		record.setUserType(null);
		record.setCreateUserId(null); 
		record.setStatus("1");
		record.setFailureCause(null);
		bisSmsRecordMapper.insert(record);
	}
	
}

	@Override
	public List<BisCustomUser> userList() {
		
		return bisCustomUserMapper.selectByExample(null);
	}
}